package com.cloudspace.jindun.activity.Woyao;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.cloudspace.jindun.R;
import com.cloudspace.jindun.activity.CustomTitleBaseActivity;
import com.cloudspace.jindun.model.RepData;
import com.cloudspace.jindun.net.manager.ImageManager;
import com.cloudspace.jindun.utils.CallingUtils;
import com.cloudspace.jindun.utils.ObjUtil;
import com.cloudspace.jindun.utils.ToastMaker;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ReportdetailsActivity extends CustomTitleBaseActivity {

    @Bind(R.id.back)
    ImageView mBack;
    @Bind(R.id.rl_text_title)
    TextView mRlTextTitle;
    @Bind(R.id.repd_tv_title)
    TextView mRepdTvTitle;
    @Bind(R.id.epd_tv_name)
    TextView mEpdTvName;
    @Bind(R.id.epd_tv_xxnr)
    TextView mEpdTvXxnr;
    @Bind(R.id.iv01)
    ImageView mIv01;
    @Bind(R.id.iv02)
    ImageView mIv02;
    @Bind(R.id.iv03)
    ImageView mIv03;
    @Bind(R.id.repd_bt_phone)
    Button mRepdBtPhone;
    @Bind(R.id.repd_bt_mess)
    Button mRepdBtMess;

    private RepData.DataBean mRepData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reportdetails, false);
        ButterKnife.bind(this);
    }

    @Override
    protected void onStart() {
        super.onStart();
        Intent intent = this.getIntent();
        mRepData = (RepData.DataBean) intent.getSerializableExtra("data");
        if (mRepData != null) {
            mRepdTvTitle.setText(mRepData.getReportName());
            mEpdTvName.setText(mRepData.getReportId());
            mEpdTvXxnr.setText(mRepData.getReportDetail());

            if (mRepData.getImageUrlThr() != null) {
                mIv01.setVisibility(View.VISIBLE);
                mIv02.setVisibility(View.VISIBLE);
                mIv03.setVisibility(View.VISIBLE);
                ImageManager.loadImage(this, mIv01, mRepData.getImageUrlOne());
                ImageManager.loadImage(this, mIv02, mRepData.getImageUrlTwo());
                ImageManager.loadImage(this, mIv03, mRepData.getImageUrlThr());

            } else if (mRepData.getImageUrlTwo() != null) {
                mIv01.setVisibility(View.VISIBLE);
                mIv02.setVisibility(View.VISIBLE);
                ImageManager.loadImage(this, mIv01, mRepData.getImageUrlOne());
                ImageManager.loadImage(this, mIv02, mRepData.getImageUrlTwo());

            } else if (mRepData.getImageUrlOne() != null) {
                mIv01.setVisibility(View.VISIBLE);
                ImageManager.loadImage(this, mIv01, mRepData.getImageUrlOne());

            }
        }
    }

    @OnClick({R.id.back, R.id.repd_bt_phone, R.id.repd_bt_mess})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.repd_bt_phone:
                callReporter();
                break;
            case R.id.repd_bt_mess:
                messageToReporter();
                break;
        }
    }

    private void messageToReporter() {
        if (ObjUtil.isEmpty(mRepData) ||
                ObjUtil.isEmpty(mRepData.getPerson()) ||
                ObjUtil.isEmpty(mRepData.getPerson().getPhoneNumber())){
            ToastMaker.showToastShort(R.string.not_has_reporter_info);
            return;
        }

        Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:"+ mRepData.getPerson().getPhoneNumber()));
        startActivity(intent);
    }

    private void callReporter() {
        if (ObjUtil.isEmpty(mRepData) ||
                ObjUtil.isEmpty(mRepData.getPerson())){
            ToastMaker.showToastShort(R.string.not_has_reporter_info);
            return;
        }

        CallingUtils.showCallMethodDialog(this, mRepData.getPerson());
    }
}
